package com.zmn.plat.admin.controller.online.service.group.product.group;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.zmn.common.constant.GlobalConsts;
import com.zmn.plat.common.dictionary.ProductDict;
import com.zmn.plat.model.entity.product.businessline.ProductBusinessLine;
import com.zmn.plat.model.entity.product.productgroup.ProductGroup;
import com.zmn.plat.model.entity.product.productgroup.ProductGroupQuery;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.stereotype.Controller;
import com.zmn.plat.model.entity.online.service.group.product.group.OnlineServiceGroupProductGroup;
import com.zmn.plat.model.entity.online.service.group.product.group.OnlineServiceGroupProductGroupQuery;

import static java.util.stream.Collectors.groupingBy;

/**
 * 在线客服分组产品组配置
 *
 * @author linfeng
 *
 * @since 2021-03-26 17:21:53
 */
@Controller
@RequestMapping("/online/service/group/product/group/")
public class OnlineServiceGroupProductGroupListController extends OnlineServiceGroupProductGroupBaseController {

    /**
     * 产品组详情
     * @param groupId
     * @param bizType
     * @return
     */
    @RequestMapping("detail")
    @RequiresPermissions("/online/service/group/product/group/detail.action")
    public ModelAndView detail(@RequestParam(value = "groupId", defaultValue = "0") Integer groupId,
                               @RequestParam(value = "bizType", defaultValue = "0") Integer bizType){

        OnlineServiceGroupProductGroupQuery serviceGroupProductGroupQuery = new OnlineServiceGroupProductGroupQuery();
        serviceGroupProductGroupQuery.setGroupId(groupId);
        serviceGroupProductGroupQuery.setBizType(bizType);
        List<OnlineServiceGroupProductGroup> selectedProductGroup = onlineServiceGroupProductGroupService.listByQuery(serviceGroupProductGroupQuery);
        List<Integer> selectedProductGroupIdList = selectedProductGroup.stream().map(e -> e.getProductGroupId()).collect(Collectors.toList());

        ProductGroupQuery productGroupQuery = new ProductGroupQuery();
        productGroupQuery.setBizType(bizType);
        productGroupQuery.setStatus(GlobalConsts.YES);
        List<ProductGroup> productGroupList = productGroupService.listByQuery(productGroupQuery);
        List<Integer> bizLineIdList = productGroupList.stream().map(e -> e.getBizLine()).distinct().collect(Collectors.toList());
        List<ProductBusinessLine> productBusinessLineList = productBusinessLineService.listByIdList(bizLineIdList);
        Map<Integer, List<ProductGroup>> productGroupMap = new HashMap<>();
        productGroupMap = productGroupList.stream().collect(groupingBy(ProductGroup::getBizLine));

        ModelAndView mav = new ModelAndView("online/service/group/product/group/detail");
        mav.addObject("groupId", groupId);
        mav.addObject("bizType", bizType);
        mav.addObject("flag", 3);
        mav.addObject("selectedProductGroupIdList", selectedProductGroupIdList);
        mav.addObject("productBusinessLineList", productBusinessLineList);
        mav.addObject("productGroupMap", productGroupMap);
        mav.addObject("bizTypeList", ProductDict.listBizType());
        return mav;
    }
}
